package com.game.data;

import com.game.config.Coefficient;
import com.game.model.map.GameMap;
import com.game.model.map.MapPoint;
import com.game.model.map.Tranfer;
import com.game.model.role.Monster;
import com.game.model.role.NPC;
import com.game.model.role.Role;
import com.game.utils.JDBCUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:地图数据
 **/
public class MapData {
    public static GameMap gameMap;//当前地图
    public static List<GameMap> mapList;//地图列表

    static {
        mapList = gameMaps();
        gameMap = getGameMapById(1);
    }

    //通过id获取地图 mysql
    public static GameMap getGameMapById(int id){
        for (GameMap g:mapList){
            if (id==g.getId()){
                return g;
            }
        }
        return null;
    }
    //通过id获取传送阵
    public static Tranfer getTranferById(int id){
        List<Tranfer> tranfers = tranfers();
        for(Tranfer t:tranfers){
            if (t.getId()==id) return t;
        }
        return null;
    }

    //获取所有的地图
    public static List<GameMap> gameMaps() {
        ArrayList<GameMap> gameMaps = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {

        } else if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from map");
                while (rs.next()) {
                    GameMap gameMap = new GameMap();
                    gameMap.setId(rs.getInt("id"));
                    gameMap.setName(rs.getString("name"));
                    int width = Integer.parseInt(rs.getString("map_point").split("-")[0]);
                    int hright = Integer.parseInt(rs.getString("map_point").split("-")[1]);
                    MapPoint[][] mapPoints = new MapPoint[width][hright];
                    ResultSet rsc = JDBCUtil.executeQuery("select * from map_point where map_id = ?", gameMap.getId());
                    while (rsc.next()) {
                        MapPoint mapPoint = new MapPoint();
                        mapPoint.setGameMapId(rsc.getInt("map_id"));
                        mapPoint.setX(rsc.getInt("x"));
                        mapPoint.setY(rsc.getInt("y"));
                        String[] point_objects = rsc.getString("point_object").split("-");
                        List<Object> objects = new ArrayList<>();
                        for (String s : point_objects) {
                            String[] object = s.split(",");
                            //如果是role类型
                            if ("role".equals(object[0])) {
                                //添加该id的role
                                objects.add(RoleData.getRoleById(Integer.parseInt(object[1])));
                            }
                            //如果是传送阵
                            if ("tranfer".equals(object[0])){
                                objects.add(MapData.getTranferById(Integer.parseInt(object[1])));
                            }
                        }
                        mapPoint.setPointObject(objects);
                        mapPoints[rsc.getInt("x")][rsc.getInt("y")] = mapPoint;
                    }
                    //添加地图点
                    gameMap.setMapPoints(mapPoints);
                    gameMaps.add(gameMap);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return gameMaps;
    }

    //获取传送阵
    public static List<Tranfer>tranfers(){
        ArrayList<Tranfer> tranfers = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)){

        }
        else if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)){
            try{
                ResultSet rs = JDBCUtil.executeQuery("select * from tranfer");
                while (rs.next()) {
                    Tranfer tranfer = new Tranfer();
                    tranfer.setId(rs.getInt("id"));
                    tranfer.setNextId(rs.getInt("next_id"));
                    tranfer.setDescription(rs.getString("description"));
                    MapPoint mapPoint = new MapPoint();
                    mapPoint.setGameMapId(rs.getInt("next_id"));
                    String[] map_points = rs.getString("map_point").split("-");
                    mapPoint.setX(Integer.parseInt(map_points[0]));
                    mapPoint.setY(Integer.parseInt(map_points[1]));
                    tranfer.setMapPoint(mapPoint);
                    tranfers.add(tranfer);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return tranfers;
    }

    //每个地图的所有坐标点集合(地图)
    public static MapPoint[][] getMapPoint(int id) {
        //新手村
        if (id == 1) {
            MapPoint[][] mapPoints = new MapPoint[10][10];
            for (int i = 0; i < mapPoints.length; i++) {
                for (int j = 0; j < mapPoints[i].length; j++) {
                    MapPoint mapPoint = new MapPoint(1, i, j);
                    mapPoints[i][j] = mapPoint;
                    if (i == 4 && j == 7) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.getRoleById(1000));
                        objects.add(RoleData.getRoleById(1001));
                        mapPoints[i][j].setPointObject(objects);
                    }
                    if (i == 2 && j == 5) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.getRoleById(1000));
                        mapPoints[i][j].setPointObject(objects);
                    }
                    //村长
                    if (i == 7 && j == 9) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.npcMap.get(100));
                        mapPoints[i][j].setPointObject(objects);
                    }

                    //王大爷
                    if (i == 7 && j == 7) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.npcMap.get(101));
                        mapPoints[i][j].setPointObject(objects);
                    }

                    //东竹林传送阵
                    if (i == 4 && j == 9) {
                        MapPoint mapPoint1 = new MapPoint(2, 4, 0);
                        Tranfer tranfer = new Tranfer(1,  2, mapPoint1, "乌漆嘛黑的竹林");
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(tranfer);
                        objects.add(RoleData.playerRole);
                        mapPoints[i][j].setPointObject(objects);
                    }
                    //黑风洞传送阵
                    if (i == 0 && j == 4) {
                        MapPoint mapPoint1 = new MapPoint(3, 0, 0);
                        Tranfer tranfer = new Tranfer(3,  3, mapPoint1, "乌漆嘛黑的洞");
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(tranfer);
                        mapPoints[i][j].setPointObject(objects);
                    }
                }
            }
            return mapPoints;
        }
        //东竹林
        if (id == 2) {
            MapPoint[][] mapPoints = new MapPoint[9][9];
            for (int i = 0; i < mapPoints.length; i++) {
                for (int j = 0; j < mapPoints[i].length; j++) {
                    MapPoint mapPoint = new MapPoint(1, i, j);
                    mapPoints[i][j] = mapPoint;
                    if (i == 4 && j == 1) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.getRoleById(1000));
                        objects.add(RoleData.getRoleById(1001));
                        mapPoints[i][j].setPointObject(objects);
                    }
                    if (i == 2 && j == 8) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.getRoleById(1000));
                        mapPoints[i][j].setPointObject(objects);
                    }
                    if (i == 4 && j == 0) {
                        MapPoint mapPoint1 = new MapPoint(1, 4, 9);
                        Tranfer tranfer = new Tranfer(2,  1, mapPoint1, "玩家开始的地方");
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(tranfer);
                        mapPoints[i][j].setPointObject(objects);
                    }
                }
            }
            return mapPoints;
        }
        //黑风洞
        if (id == 3) {
            MapPoint[][] mapPoints = new MapPoint[5][5];
            for (int i = 0; i < mapPoints.length; i++) {
                for (int j = 0; j < mapPoints[i].length; j++) {
                    MapPoint mapPoint = new MapPoint(1, i, j);
                    mapPoints[i][j] = mapPoint;
                    if (i == 4 && j == 1) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.getRoleById(1000));
                        objects.add(RoleData.getRoleById(1001));
                        mapPoints[i][j].setPointObject(objects);
                    }
                    if (i == 2 && j == 1) {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(RoleData.getRoleById(1000));
                        mapPoints[i][j].setPointObject(objects);
                    }
                    if (i == 0 && j == 0) {
                        MapPoint mapPoint1 = new MapPoint(1, 0, 4);
                        Tranfer tranfer = new Tranfer(2,  1, mapPoint1, "玩家开始的地方");
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(tranfer);
                        mapPoints[i][j].setPointObject(objects);
                    }
                }
            }
            return mapPoints;
        }
        return null;
    }

    /**
     * @param name   地图名称
     * @param width  长
     * @param height 宽
     * @return 是否创建成功
     */
    public static boolean creatGameMap(int id, String name, int width, int height) {
        try {
            Integer integer = JDBCUtil.executeDML("insert into map (id,name,map_point) values(?,?,?)", id, name, "" + width + "-" + height);
            if (integer < 1) return false;
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    JDBCUtil.executeDML("insert into map_point(map_id,x,y)values(?,?,?)", id, i, j);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //设置地图点物体
    public static boolean setMapPoint(int id, int x, int y, Object ob) {
        try {
            String point_object = "";
            ResultSet rs = JDBCUtil.executeQuery("select point_object from map_point where map_id=? and x=? and y=?", id, x, y);
            while (rs.next()) {
                point_object = rs.getString("point_object");
            }
            if (ob instanceof Role) {
                point_object += "-role," + ((Role) ob).getId();
            } else if (ob instanceof  Tranfer) {
                point_object += "-tranfer,"+((Tranfer) ob).getId();
            } else {
                return false;
            }
            if ("-".equals(point_object.substring(0, 1))) {
                point_object = point_object.replaceFirst("-", "");
            }
            Integer integer = JDBCUtil.executeDML("update map_point set point_object = ? where map_id=? and x=? and y=?", point_object, id, x, y);
            if (integer < 1) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
